<?php
namespace Common\Taglib;
using \Think\Template\TagLib;
/**
 * Class TagLibSysattribute
 * @since 1.0 2014-11-11 by sutroon
 */
class TagLibExfield extends TagLibBase {

    protected $tags = array(
        'formfield' => array('attr' => 'attrs', 'close' => 0),
        'list' => array('attr' => 'attrs', 'close' => 1)
    );

    /**
     * 表单字段
     * @param type $attr
     * @param type $content
     * @return string
     * @since 1.0 2014-11-11 by sutroon; 2.0 2014-12-13 by sutroon
     * @example <exfield:formfield target="CUSTOMER" siteid="{$data.siteID}" itemid="{$data.id}" />
     */
    function _formfield($attr, $content) {
        $M = new Model();
        $attr = $this->parseXmlAttr($attr, 'formfield');
        $siteID = $this->base_get_tpl_data($attr['siteid']);
        $itemID = empty($attr['itemid']) ? 0 : $attr['itemid'];
        $target = $attr['target'];
        $list = sofunc_get_sys_exfield_list($target, $siteID);
        $data = false;
        if ($itemID) {
            $itemID = $this->base_get_tpl_data($itemID);
            $data = sofunc_get_sys_exfield_data($target, $siteID, $itemID);
        }
        $str = '';
        $i = 0;
        $itr = 0;
        $colcount = 3;
        $cellcount = count($list);
        $trcount = floor($cellcount / $colcount);
        $padcount = $cellcount % $colcount;
        foreach ($list as $row) {
            if ($i == 0 || $i % $colcount == 0) {
                if ($i > 0) {
                    $str.='</tr>';
                    $itr++;
                }
                $str .= '<tr>';
            }
            $label = $row['label'];
            $field = $row['field'];
            $value = $data ? $data[$field] : '';
            $isRequire = $row['isRequire'] ? ' required="required"' : '';
            if ($padcount > 0 && $istr > 0 && $itr == $trcount && $i == ($cellcount - 1)) {
                $str.='<td class="notbreakline" colspan="' . ($colcount - $padcount + 1) . '">';
            } else {
                $str.='<td class="notbreakline">';
            }
            $str .= '<label>' . $label . '：</label>';
            switch ($row['inputType']) {
                case 'text':
                    $str .= '<input type="text" name="' . $field . '" value="' . $value . '" placeholder="' . $label . '"' . $isRequire . ' />';
                    break;
                case 'date':
                    $str .= '<input type="text" name="' . $field . '" value="' . $value . '" placeholder="' . $label . '"' . $isRequire . ' class="datepicker" />';
                    break;
                case 'select':
                    $str .= '<select name="' . $field . '"' . $isRequire . '><option value="">请选择</option>';
                    $items = explode(',', $row['optionalValue']);
                    if ($items) {
                        foreach ($items as $item) {
                            if (false !== ($pos = strpos($item, '='))) {
                                $val = substr($item, 0, $pos);
                                $str .= '<option value="' . $val . '"' . ($data && $val == $data[$field] ? ' selected="selected"' : '') . '>' . substr($item, $pos + 1) . '</option>';
                                continue;
                            }
                            $str .= '<option value="' . $item . '"' . ($data && $item == $data[$field] ? ' selected="selected"' : '') . '>' . $item . '</option>';
                        }
                    }
                    $str .= '</select>';
                    break;
                case 'radio':
                    $items = explode(',', $row['optionalValue']);
                    if ($items) {
                        foreach ($items as $item) {
                            if (false !== ($pos = strpos($item, '='))) {
                                $val = substr($item, 0, $pos);
                                $str .= '<input type="radio" name="' . $field . '" value="' . $val . '"' . ($data && $val == $data[$field] ? ' checked="checked"' : '') . $isRequire . ' />' . substr($item, $pos + 1);
                                continue;
                            }
                            $str .= '<input type="radio" name="' . $field . '" value="' . $item . '"' . ($data && $item == $data[$field] ? ' checked="checked"' : '') . $isRequire . ' />' . $item;
                        }
                    }
                    break;
                case 'checkbox':
                    $items = explode(',', $row['optionalValue']);
                    $values = $value ? explode(',', $value) : false;
                    if ($items) {
                        foreach ($items as $item) {
                            if (false !== ($pos = strpos($item, '='))) {
                                $val = substr($item, 0, $pos);
                                $str .= '<input type="checkbox" name="' . $field . '[]" value="' . $val . '"' . (($values && in_array($val, $values)) ? ' checked="checked"' : '') . $isRequire . ' />' . substr($item, $pos + 1);
                                continue;
                            }
                            $str .= '<input type="checkbox" name="' . $field . '[]" value="' . $item . '"' . (($values && in_array($item, $values)) ? ' checked="checked"' : '') . $isRequire . ' />' . $item;
                        }
                    }
                    break;
                case 'textarea':
                    $str .= '<textarea name="' . $field . '" cols="60" rows="3" placeholder="' . $label . '"' . $isRequire . '>' . $value . '</textarea>';
                    break;
                case 'editor':
                    $str .= '<textarea name="' . $field . '" class="editor" placeholder="' . $label . '">' . $value . '</textarea>';
                    break;
                case 'file':
                    $str .= '<input type="file" name="' . $field . '" placeholder="' . $label . '"' . $isRequire . ' />';
                    break;
            }
            $str.='</td>';
            $i++;
        }
        if ($str) {
            $str = '<table' . $this->base_attr_to_string($attr, array('siteid', 'itemid', 'target')) . '>' . $str . '</tr></table>';
        }
        return $str;
    }

}
